package org.gavin.controller;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.gavin.core.annotation.AccessLimit;
import org.gavin.core.annotation.LogRecord;
import org.gavin.core.annotation.Safety;
import org.gavin.core.domain.R;
import org.gavin.entity.dto.Recommend;
import org.gavin.entity.param.RecommendParam;
import org.gavin.entity.param.TestParam;
import org.gavin.enums.RecommendEnums;
import org.gavin.service.AbsentLoaderService;
import org.gavin.service.RecommendService;
import org.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.retry.annotation.Retryable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class DomeController {

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private AbsentLoaderService loaderService;

    @Autowired
    private RedisService redisService;

    @Value("${video.recommend.view.total:20}")
    private Integer totalCount;

    @Safety
    @PostMapping(value = "/test/log")
    public R<JSONObject> getLogs(@RequestBody TestParam param) {
        JSONObject json = new JSONObject();
        json.put("aa", "bb");
        json.put("bb", param.getDateTime());
        json.put("age", param.getAge());
        log.error("==========================");
        return R.ok(json);

    }

    @Retryable
    @LogRecord(description = "初始化数据")
    @GetMapping(value = "/init/list")
    public R<List<Integer>> getList() {
        recommendService.initRecommendData();
        return R.ok();
    }

    @PostMapping(value = "/random/list")
    public R<List<Integer>> getRandomList(@RequestBody RecommendParam param) {
        RecommendEnums testData = RecommendEnums.TEST_DATA;
        String andLoader = redisService.getAndAsyncLoader(testData,loaderService.incrementLoader());
        List<Integer> list = new ArrayList<>();
        list.add(StringUtils.isNotBlank(andLoader)?Integer.parseInt(andLoader):0);
        return R.ok(list);
        //return R.ok(recommendService.getRecommendData(param));
    }

    @AccessLimit(period = 30, count = 2)
    @PostMapping(value = "/random/lists")
    public R<List<Integer>> getRandomLists(@RequestBody RecommendParam param) {
        return R.ok(recommendService.getRecommendData(param));
    }


    @PostMapping(value = "/random/list/all")
    public R<Map<Integer, List<Integer>>> getAllRandomList(@RequestBody RecommendParam param) {
        Map<Integer, List<Integer>> result = new HashMap<>(32);
        Integer loopTimes = 0;
        List<Integer> recommendData = recommendService.getRecommendData(param);
        result.put(loopTimes, recommendData);
        if (totalCount <= recommendData.size()) {
            for (int i = 1; i < 20; i++) {
                List<Integer> recommendData1 = recommendService.getRecommendData(param);
                result.put(i, recommendData1);
                if (totalCount > recommendData1.size()) {
                    break;
                }
            }
        }
        return R.ok(result);
    }


    @PostMapping(value = "/add/random/data")
    public R<String> addData(@RequestBody Recommend param) {
        // 添加 需要 加入到最新数据列表
        recommendService.addRecommendData(param);
        return R.ok();
    }

    @PostMapping(value = "/delete/random/data")
    public R<String> deleteData(@RequestBody Recommend param) {
        // 添加 需要 加入到最新数据列表
        recommendService.deleteRecommendData(param);
        return R.ok();
    }

    @PostMapping(value = "/top/random/data")
    public R<String> addTopData(@RequestBody Recommend param) {
        // 添加 需要 加入到最新数据列表
        recommendService.addTopped(param);
        return R.ok();
    }

    @PostMapping(value = "/remove/top/random/data")
    public R<String> removeTopData(@RequestBody Recommend param) {
        // 添加 需要 加入到最新数据列表
        recommendService.removeTopped(param);
        return R.ok();
    }
}
